package com.toppitch.site.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.toppitch.site.data.HedgeFund;
import com.toppitch.site.data.JsonData;
import com.toppitch.site.db.DBManager;

@Controller
@RequestMapping("/")
public class HFController extends UserBaseController {
	@Autowired
	private DBManager dbmanager;

	@RequestMapping("hf/register")
	public String register(//
			@RequestParam("name") String name,//
			@RequestParam("email") String email,//
			@RequestParam("password") String password,//
			@RequestParam("photoId") String photoId,//
			HttpSession session, ModelMap model) {
		setupNYTNews(model);
		JsonData json = new JsonData();
		model.addAttribute("jsonData", json);

		if (session.getAttribute("HF") == null) {
			// register
			HedgeFund hf = new HedgeFund();
			dbmanager.queryFirst(hf, "select * from hf where lower(email)=?", email.toLowerCase());
			if (email.equalsIgnoreCase(hf.getEmail())) {
				json.setSucceed(false);
				json.setErrorMessage("Email address:" + email + " is already registered.");

				return "json";
			}

			HedgeFund fund = new HedgeFund();
			fund.setName(name);
			fund.setEmail(email);
			fund.setPassword(password);
			fund.setPhotoId(photoId);
			json.setSucceed(dbmanager.execute(fund.toInsertString()));
			if (json.isSucceed()) {
				session.setAttribute("HF", fund);
			} else {
				json.setErrorMessage("Failed to update database");
			}
		} else {
			HedgeFund fund = (HedgeFund) session.getAttribute("HF");
			fund.setName(name);
			fund.setPassword(password);
			fund.setPhotoId(photoId);
			json.setSucceed(dbmanager.execute(fund.toUpdateString()));
			if (json.isSucceed()) {
				session.setAttribute("HF", fund);
			} else {
				json.setErrorMessage("Failed to update database");
			}
		}

		return "json";
	}

	@RequestMapping("hfHome")
	public String home(
	//
			@RequestParam(value = "page", defaultValue = "1") String page,//
			@RequestParam(value = "count", defaultValue = "10") String count,//
			@RequestParam(value = "searchWords", defaultValue = "") String searchWords,//
			HttpSession session, ModelMap model) {
		setupNYTNews(model);
		Object hf = session.getAttribute("HF");
		if (hf != null && hf instanceof HedgeFund) {
			model.addAttribute("user", hf);

			home(page, count, searchWords, model, false, null);

			return "hf/home";
		}
		return "login";
	}

	@RequestMapping("hfProfile")
	public String profile(HttpSession session, ModelMap model) {
		setupNYTNews(model);

		Object hf = session.getAttribute("HF");
		if (hf != null && hf instanceof HedgeFund) {
			model.addAttribute("user", hf);
			model.addAttribute("hf", hf);

			return "hf/profile";
		}
		return "login";
	}

	@RequestMapping("hfEditProfile")
	public String editProfile(HttpSession session, ModelMap model) {
		setupNYTNews(model);

		Object hf = session.getAttribute("HF");
		if (hf != null && hf instanceof HedgeFund) {
			model.addAttribute("user", hf);
			model.addAttribute("hf", hf);

			return "hf/editProfile";
		}
		return "login";
	}
}
